package com.wlz.provider.service;

import com.wlz.api.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.config.annotation.Service;
import org.apache.dubbo.rpc.RpcContext;

import java.util.concurrent.CompletableFuture;

/**
 *  异步调用
 * @author wlz
 * @date 2022-07-12  11:56 下午
 */
@Slf4j
@Service(version = "async")
public class AsyncDemoService implements DemoService {
    @Override
    public String sayHello(String name) {
        log.info("AsyncDemoService 执行了同步服务 {}", name);
        URL url = RpcContext.getContext().getUrl();
        return String.format("%s：%s, Hello, %s", url.getProtocol(), url.getPort(), name);  // 正常访问
    }

    @Override
    public CompletableFuture<String> sayHelloAsync(String name) {
        log.info("AsyncDemoService 执行了异步服务 {}", name);
        return CompletableFuture.supplyAsync(() -> {
            return "sayhello "  + name;
        });
    }

}
